<?php
class WeathAction extends BasePlusAction {
	public $city ,$keyword;

  ///插件版本号
    public function ver()
    {
      return "20131220";
    }
    public function code()
    {
      return "Weath";
    }

    public function desc()
    {
      return "微信查询天气加强版,可以返回文本模板或图文格式.默认是文本格式(推荐)";
    }


    public function title()
    {
      return "天气查询";
  	}



  	public function initmsg()
  	{
  		if ($this->wxpost['Content'] == "天气") {
  			$this->wxpost['Content'] = $this->wxpost['Content']." ".$this->myPlus['city'];
  		}
  		$keyreplace =  preg_replace("/[\ |\@|\-]/", '_', $this->wxpost['Content']);
  		
        $keyarr = explode("_", $keyreplace);
        if (count($keyarr) != 2) {
        	return false;
        }
  		$this->city = $keyarr[1];
  		$this->city = str_replace("市", "", $this->city);
        return true;
  	}

  	public function getMsg($value='')
  	{
  		if (!$this->initmsg()) return false;
  		if ($this->myPlus['msgtype'] == 'text') {
  			$this->textmsg();
  		}elseif ($this->myPlus['msgtype'] == 'news') {
  			$this->newsmsg();
  		}
  		return $this->retMsg;
  	}

  	public function textmsg()
  	{
  		$api = "http://api.map.baidu.com/telematics/v3/weather?location=".$this->city."&output=json&ak=aMCbqrEK9HoSjGA3dkSGQkqO";
  		$rs = callhttp($api);
  		$json = json_decode($rs,true);
  		if ($json['status'] != "success") return false;
  		$msg .=$this->city;
  		foreach ($json['results'][0]['weather_data'] as $k) {
  			$msg .= $k['date'].' '.$k['weather'].$k['temperature']."\n";
  		}
  		$this->retMsg['msgType'] = 'text';
  		$this->retMsg['msg'] = $msg;
  	}

  	public function newsmsg(){
  		$api = "http://api.map.baidu.com/telematics/v3/weather?location=".$this->city."&output=json&ak=aMCbqrEK9HoSjGA3dkSGQkqO";
  		$rs = callhttp($api);
  		$json = json_decode($rs,true);
  		if ($json['status'] != "success") return false;
  		$volist = array();
  		foreach ($json['results'][0]['weather_data'] as $k) {
  			$vo = array();
  			$vo['title'] = $this->city.' '.$k['date'].' '.$k['weather'].$k['temperature'];
  			$vo['url'] = '';
  			$vo['picurl'] = $k['dayPictureUrl'];
  			$vo['desc'] = $k['date'].' '.$k['weather'].$k['temperature'];
  			$volist[] = $vo;
  		}
  		$this->retMsg['msgType'] = 'news';
  		$this->retMsg['volist'] = $volist;
  	}

}